<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="300px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title-text">智能协同云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto" class="menu-title">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col>
        <div class="user-login-status" flex="120px">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '未命名用户' }}
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="toPersonCenter">
                    <LogoutOutlined />
                    个人中心
                  </a-menu-item>
                  <a-menu-item @click="toUpdatePassword">
                    <LogoutOutlined />
                    修改密码
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>

          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { userLogoutUsingPost } from '@/api/userController'
const loginUserStore = useLoginUserStore()

const current = ref<string[]>([''])
// 菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    icon: () => h(UserOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    icon: () => h(UserOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/addPicture',
    icon: () => h(UserOutlined),
    label: '图片上传',
    title: '图片上传',
  },

  {
    key: '/other',
    icon: () => h(SettingOutlined),
    label: '其他',
    title: '其他',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

const toPersonCenter =  () => {
   router.push('/user/personCenter')
}
const toUpdatePassword = () => {
  router.push('/user/updatePassword')
}
const router = useRouter()
// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
// 监听路由变化，更新高亮显示的菜单
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
</script>
<style scoped>
::v-deep(.ant-menu-item, .ant-dropdown-menu-item) {
}
.title-bar {
  display: flex;
  align-items: center;
}
.logo {
  height: 50px;
}

.title-text {
  color: black;
  margin-left: 15px;
  font-weight: bold;
  font-size: 15px;
}
.user-login-status {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  white-space: nowrap; /* 防止换行 */
  overflow: hidden; /* 隐藏溢出内容 */
}

.user-info {
  display: flex;
  align-items: center;
  overflow: hidden; /* 隐藏溢出内容 */
}

.username {
  margin-left: 8px; /* 提供更好的间距 */
  text-overflow: ellipsis; /* 超出部分用省略号表示 */
  white-space: nowrap; /* 防止换行 */
  overflow: hidden; /* 隐藏溢出内容 */
}

/* 媒体查询: 小屏幕设备 */
@media (max-width: 768px) {
  .username {
    display: none; /* 在小屏幕上隐藏用户名 */
  }

  .user-info {
    flex-direction: row; /* 确保头像和用户名在同一列 */
  }
}
.avatar {
  width: 40px;
  height: 40px;
}
</style>
